*** Jetson Leder-Luis
*** January, 2023
*** Can Whistleblowers Root Out Public Expenditure Fraud? Evidence from Medicare

*** Prepared for REStat 

*** This file analyzes health effects in Appendix G


global basepath "/homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/kyphohealth"
global codepath "$basepath/code"
global logpath "$codepath/logs"
global resultspath "$basepath/results"
global datapath "$basepath/data"
global rawpath "/disk/aging/medicare/data/harm/100pct"

set maxvar 12000

log using "$logpath/KyphoHealthAnalyze.log", replace


use $datapath/osteopanel_death.dta, clear 

* Descriptor
count
* 44.37m 


******************************************************************
****** Prep Claim Flags
****************************************************************** 

gen anyspineOP = 1 if surgicalspine >= 1 & !mi(surgicalspine)
replace anyspineOP = 0 if mi(anyspineOP)

gen anykyphoIP = 1 if kyphostay >= 1 & !mi(kyphostay)
replace anykyphoIP = 0 if mi(anykyphoIP)

gen treated = 1 if anyspineOP ==1 | anykyphoIP == 1 


******************************************************************
****** Time analysis of claims
****************************************************************** 

** Filed 06, settled 08 
** Make plot of patients over years
preserve
gen count = 1 
fcollapse (sum) anyspineOP (sum) anykyphoIP (sum) count (sum) treated, by(year) 

keep if year >= 2002 & year <= 2012 
	* 4 years before/after case 
twoway (connected anykyphoIP year) (connected anyspineOP year) (connected treated year), xline(2006, lcolor(black)) xline(2008, lcolor(black)) graphregion(color(white)) xtitle("Year") ytitle("Number of Osteo Patients Treated") legend(off) yscale(range(0 .015)) ylab(#3) yscale(range(0 2200)) saving("$resultspath/TreatedPts.gph", replace) ysize(5) xsize(7) text(33500 2011 "Treated IP or OP", color(forest_green) place(w)) text(26000 2011 "Treated Outpatient", color(maroon)) text(10500 2011 "Treated Inpatient", color(dknavy))

graph export "$resultspath/TreatedPts.pdf", replace 
restore 


***********************************************
********** Cohort study of death and treatment 
************************************************

** Prep cohort variables 


sort bene_id year 
by bene_id: egen baseyear = min(year)
gen cohortyear = year - baseyear 

* make cc binary
foreach varname in alzh ami atrialfb cataract chrnkidn copd chf diabetes glaucoma hipfrac ischmcht depressn osteoprs ra_oa strketia cncrbrst cncrclrc cncrprst cncrlung cncrendm anemia  asthma hyperl hyperp hypert hypoth{
	replace `varname' = 1 if inlist(`varname', 1, 3)
	replace `varname' = 0 if inlist(`varname', 2, 4)
}


** Fix types 
encode race, gen(r)
drop race 
rename r race

gen zip5 =substr(bene_zip, 1, 5)
destring zip5, replace force

** make baseline variables 
foreach varname in age race zip5 alzh ami atrialfb cataract chrnkidn copd chf diabetes glaucoma hipfrac ischmcht depressn osteoprs ra_oa strketia cncrbrst cncrclrc cncrprst cncrlung cncrendm anemia  asthma hyperl hyperp hypert hypoth{
	gen baseline`varname' = `varname' if year == baseyear
} 

save $datapath/osteopanel_death_prepped.dta, replace 



use $datapath/osteopanel_death_prepped.dta, clear 

**********************************
* 5- year window
*********************************
preserve
keep if cohortyear <= 4 

gen death5 = 1 if year(deathdateclean) - baseyear <= 4
replace death5 = 0 if mi(death5)

drop if baseyear > 2011


** One line per patient 
collapse (max) anykyphoIP (max) anyspineOP (first) base* (max) death5, by(bene_id)
count 


************************
********** Regression 
** Regress
reghdfe death5 anykyphoIP##ib2005.baseyear anyspineOP##ib2005.baseyear, absorb(i.baseline*)
est sto A 
esttab A using "$resultspath/Reg_5year.tex", se replace

** Tag death interacted with treatment 

gen died5IP = 1 if death5 == 1 & anykyphoIP == 1 
replace died5IP = 0 if mi(died5IP)

gen died5OP = 1 if death5 == 1 & anyspineOP == 1 
replace died5OP = 0 if mi(died5OP)

gen treated = 1 if (anykyphoIP == 1)  | (anyspineOP == 1)
replace treated = 0 if mi(treated)

gen diedtreated = 1 if (death5 == 1) & (treated == 1)
replace diedtreated = 0 if mi(diedtreated)



*******************
** Create cohort graph 

gen benes = 1 

collapse (sum) anykyphoIP (sum) anyspineOP (sum) death5 (sum) died5IP (sum) died5OP (sum) treated (sum) diedtreated (sum) benes, by(baseyear)

gen shareIP = anykyphoIP/bene
gen shareOP = anyspineOP/bene

gen sharediedIP = died5IP/anykyphoIP
gen sharediedOP = died5OP/anyspineOP
gen sharedied = death5/benes
gen sharediedtreated = diedtreated/treated

twoway (connected sharediedIP baseyear) (connected sharediedOP baseyear) (connected sharediedtreated baseyear) (connected sharedied baseyear) ,  xline(2006, lcolor(black)) xline(2008, lcolor(black)) graphregion(color(white)) xtitle("Cohort Year") ytitle("Share Died Within 5 Years of Diagnosis") saving("$resultspath/CohortDeath5.gph", replace) text( 0.45 2011 "Treated Inpatient", color(dknavy) size(small) place(n)) text( 0.41 2011.5 "Treated IP or OP", color(forest_green) size(small) place(s)) text( 0.36 2011.3 "Treated Outpatient", color(maroon) size(small) place(s)) text( 0.31 2011 "All Osteoporosis", color(orange) size(small) place(s)) legend(off)
// legend(label(1 "Treated Inpatient") label(2 "Treated Outpatient") label(3 "Treated IP or OP") label(4 "All Osteoporosis")) 

graph export "$resultspath/CohortDeath5.pdf", replace

restore 

